﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
///DBedna 的摘要说明
/// </summary>
public class DBedna
{
    public DBedna()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// Edna 数据库测试连接
    /// </summary>
    /// <param name="serverName">服务器IP</param>
    /// <param name="port">端口号</param>
    /// <returns>200:error,i=0:ok</returns>
    static public int testCon(string serverName, string port)
    {
        int i = 200;
        ELink.declare eDNA = new ELink.declare();
        try
        {
            i = eDNA.ConnectToServer(ref serverName, ref port);
        }
        catch (Exception ce)
        {
            throw new Exception(ce.Message);
        }
        return i;
    }

    /// <summary>
    /// Open eDNA
    /// </summary>
    /// <returns></returns>
    static public int getConToServer()
    {
        string ip = ConfigurationManager.AppSettings["EIP"].ToString();
        string port = ConfigurationManager.AppSettings["EPort"].ToString();

        ELink.declare eDNA = new ELink.declare();

        return eDNA.ConnectToServer(ref ip, ref port);
    }

    /// <summary>
    /// Close eDNA
    /// </summary>
    static public void closeConToServer()
    {
        ELink.declare eDNA = new ELink.declare();

        eDNA.DisConnect();
    }

    /// <summary>
    /// 根据点名取到实时的值
    /// </summary>
    /// <param name="tagName">测点名</param>
    /// <param name="drval">值</param>
    /// <returns></returns>
    static public string GetCurValue(ref string tagName)
    {
        object drval = 0;

        ELink.declare eDNA = new ELink.declare();

        eDNA.GetCurValue(ref tagName, ref drval);

        return drval.ToString();

    }

    /// <summary>
    /// 根据测点名取到值
    /// </summary>
    /// <param name="tagName"></param>
    /// <param name="time"></param>
    /// <param name="drval"></param>
    /// <returns></returns>
    static public string GetHisValue(ref string tagName, ref string time)
    {
        object drval = 0;

        ELink.declare eDNA = new ELink.declare();

        //eDNA.GetHistValue(ref tagName, ref  time, ref  drval);

        return drval.ToString();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="tagName"></param>
    /// <returns></returns>
    public double GetCurValueByTag(string tagName)
    {
        object drval = 0;

        ELink.declare eDNA = new ELink.declare();

        eDNA.GetCurValue(ref tagName, ref drval);

        return double.Parse(drval.ToString());
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="tagName"></param>
    /// <param name="time"></param>
    /// <returns></returns>
    public double GetHisValueByTag(string tagName, string time)
    {
        object drval = 0;
       // time = "2011-10-26 09:30:00";
        ELink.declare eDNA = new ELink.declare();

        //int i = eDNA.GetHistValue(ref tagName, ref  time, ref  drval);

        return double.Parse(drval.ToString());
    }

    /// <summary>
    /// 计算点不同时间差值
    /// </summary>
    /// <param name="tName">点名</param>
    /// <param name="bTime">开始时间</param>
    /// <param name="eTime">结束时间</param>
    /// <returns></returns>
    public string DiffValeTag(string tName, string bTime, string eTime)
    {
        double bValue = 0;
        double eValue = 0;
        double Diff = 0;

        DBedna edna = new DBedna();

        bValue = edna.GetHisValueByTag(tName, bTime);
        eValue = edna.GetHisValueByTag(tName, eTime);

        Diff = eValue - bValue;

        return Diff.ToString();

    }

    /// <summary>
    /// 计算点不同时间差值
    /// </summary>
    /// <param name="tName">点名</param>
    /// <param name="bTime">开始时间</param>
    /// <returns></returns>
    public string DiffValeTag(string tName, string bTime)
    {
        double bValue = 0;
        double eValue = 0;
        double Diff = 0;

        DBedna edna = new DBedna();

        bValue = edna.GetHisValueByTag(tName, bTime);
        eValue = edna.GetCurValueByTag(tName);

        Diff = eValue - bValue;

        return Diff.ToString();

    }
}
